import {DefineVjsonComponentOptions, PropertyDefine, VjsonComponent, VjsonDefine} from "./DefineVjsonComponent";
import {getLayout, registeVjsonLayout} from "./CmpStore";
import _ from "lodash";
import {Component} from "vue/dist/vue";

export type LayoutOutLineDefine = {
    /**
     * 属性描述
     */
    label: string

    /**
     * 是否数组列表
     */
    isList: boolean
}

export type LayoutPropertyDefine = {
    /**
     * 顺序
     */
    order: number

    /**
     * 属性描述
     */
    desc: string
}

export type LayoutEventDefine = {
    /**
     * 顺序
     */
    order: number

    /**
     * 属性描述
     */
    desc: string
}

export type LayoutOutLineDefineObject = {
    [K: string]: LayoutOutLineDefine
}
export type LayoutPropertyDefineObject = {
    [K: string]: LayoutPropertyDefine
}
export type LayoutEventDefineObject = {
    [K: string]: LayoutEventDefine
}

export type LayoutSaveHandler = (vexport: any, vjson: any) => void

export type LayoutLoadHandler = (vexport: any, vjson: any) => void

interface DefineVjsonLayoutOptions {
    name: string
    layoutType: 'PC' | 'MOBILE'
    designSupport: boolean
    runtime: Component | string
    designer: Component | string
    outline: LayoutOutLineDefineObject
    install?: (Vue: any) => void
    emptyTemplate?: any
    props?: LayoutPropertyDefineObject
    events?: LayoutEventDefineObject
    saveHandler?: LayoutSaveHandler
    loadHandler?: LayoutLoadHandler
}


export class VjsonLayout {
    layoutTag: string
    options: DefineVjsonLayoutOptions

    constructor(layoutTag: string, options: DefineVjsonLayoutOptions) {
        this.layoutTag = layoutTag
        this.options = options
    }

    install(Vue) {
        if (typeof this.options.install === 'function') {
            this.options.install(Vue)
        }
    }
}

export default function defineVjsonLayout(layoutTag: string, options: DefineVjsonLayoutOptions) {
    if (getLayout(layoutTag)) {
        throw new Error(`布局${layoutTag}已经注册过`)
    }
    const cmp = new VjsonLayout(layoutTag, options)
    registeVjsonLayout(cmp)
    return cmp
}